package com.lei.demo.controller;

import com.lei.demo.entity.User;
import com.lei.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Shinelon on 2017/6/9.
 */
@Controller
@RequestMapping("/user")
public class UserController {



    @Autowired
    private UserService userService;

    @RequestMapping(value = "/",method = {RequestMethod.GET,RequestMethod.POST})
    public String showUserForm(Model model) {
        model.addAttribute("user", new User());
        return "login";
    }

    @RequestMapping(value = "/create", method = {RequestMethod.GET,RequestMethod.POST})
    public String saveUser( Model model, User user) {
        User existing = userService.findByName(user.getName());
        if(existing != null) {
            model.addAttribute("status", "exist");
            return "index";
        }
        userService.saveUser(user);
        model.addAttribute("saved","success");
        return "index";
    }

    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public String searchUser(Model model, User user) {
        List<User> users = userService.findUsers(user.getName());
        model.addAttribute("users",users);
        return "index";
    }

    @RequestMapping(value = "/login",method = {RequestMethod.GET,RequestMethod.POST})
    public String Login(Model model,User user ,HttpServletRequest request) {
        User existing = userService.findByName(user.getName());
        if(existing == null) {
            model.addAttribute("status", "not exist");
            return "login";
        }
        if(!existing.getPwd().equals(user.getPwd())) {
            model.addAttribute("status","pwderror");
            return "login";
        }
        model.addAttribute("teacher", existing);
        model.addAttribute("cw", existing);
        request.getSession().setAttribute("user", user);
        if(existing.getType().equals("teacher")) {
            return "TripForm_new";
        } else if (existing.getType().equals("cw")) {
            return "budget_new";
        } else {
            return "index";
        }
    }

    @RequestMapping(value = "/edit/{name}", method = RequestMethod.GET)
    public String updateUser(Model model, @PathVariable String name) {
        User user = userService.findByName(name);
        model.addAttribute("user", user);
        return "update";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String updateUser( Model model, User user) {
        user.setId(user.getId());
        userService.saveUser(user);
        model.addAttribute("saved","success");
        return "update";
    }

    @RequestMapping(value = "/delete/{name}", method = RequestMethod.GET)
    public String deleteUser(Model model, @PathVariable String name) {
        userService.deleteUser(name);
        model.addAttribute("deleted", "success");
        model.addAttribute("user", new User());
        return "index";
    }



}
